using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class CachDungDAO
    {
        DataProvider Provider;
        public CachDungDAO()
        {
            Provider = new DataProvider();
        }

        public DataTable LayMaCachDung()
        {
            Provider.Connect();
            string str = "Select MaCD From CachDung Where MaCD >= all (Select MaCD From CachDung)";
            DataTable dt = new DataTable();
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }

        public DataTable LayBangCachDung()
        {
            Provider.Connect();
            string str = "select * from CachDung";
            DataTable dt = new DataTable();
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }


        public void ThemCachDung(CachDungDTO cdDto)
        {
            Provider.Connect();
            string str;
            str = "Insert into CachDung(TenCD, MaCD) values (?, ?)";

            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);
            cmd.Parameters.Add("@TenCD", OleDbType.WChar);
            cmd.Parameters.Add("@MaCD", OleDbType.WChar);

            cmd.Parameters["@TenCD"].Value = cdDto.TenCD;
            cmd.Parameters["@MaCD"].Value = cdDto.MaCD;

            cmd.ExecuteNonQuery();
            Provider.Disconnect();

        }


        public void XoaCachDung(string maCD)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            Provider.Connect();
            // B3: Tao chuoi strSQL thao tac CSDL
            string str;
            str = "Delete From CachDung Where MaCD = ?";
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);
            cmd.Parameters.Add("@MaCD", OleDbType.Char);
            cmd.Parameters["@MaCD"].Value = maCD;
            cmd.ExecuteNonQuery();
            // B5: Dong ket noi CSDL
            Provider.Disconnect();
        }

        public void SuaCachDung(CachDungDTO cdDto)
        {
            Provider.Connect();
            // B3: Tao chuoi strSQL thao tac CSDL
            string str;
            str = "Update CachDung Set TenCD = ? Where MaCD = ? ";
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);

            cmd.Parameters.Add("@TenCD", OleDbType.WChar);
            cmd.Parameters.Add("@MaCD", OleDbType.WChar);

            cmd.Parameters["@TenCD"].Value = cdDto.TenCD;
            cmd.Parameters["@MaCD"].Value = cdDto.MaCD;

            cmd.ExecuteNonQuery();
            Provider.Disconnect();
        }


        public DataTable TimKiemTheoMaCD(string maCD)
        {
            Provider.Connect();
            DataTable dt = new DataTable();
            //B3: Tao chuoi strSQL thao tac CSDL
            string str = "Select * From CachDung Where MaCD = " + "'" + maCD + "'";
            //B4: Thuc thi chuoi strSQL
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);

            cmd.Parameters.Add("@MaCD", OleDbType.WChar);
            cmd.Parameters["@MaCD"].Value = maCD;

            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }
    }
}
